<div class="wrapper clearfix">
	<div class="position mt_10"> <span>您当前的位置：</span> <a href="{url:}"> 首页</a> » 购物车</div>

	<div id="cart_prompt" class="cart_prompt f14 t_l" {if:empty($this->promotion)}style="display:none"{/if}>
		<p class="m_10 gray"><b class="orange">恭喜，</b>您的订单已经满足了以下优惠活动！</p>
		{foreach:items = $this->promotion}
		<p class="indent blue">{$item['plan']}，{$item['info']}</p>
		{/foreach}
	</div>

	<table width="100%" class="cart_table m_10">
		<col width="115px" />
		<col />
		<col width="80px" />
		<col width="80px" />
		<col width="80px" />
		<col width="80px" />
		<col width="80px" />
		<col width="80px" />
		<caption>查看购物车</caption>
		<thead>
			<tr><th>图片</th><th>商品名称</th><th>赠送积分</th><th>单价</th><th>优惠</th><th>数量</th><th>小计</th><th class="last">操作</th></tr>
		</thead>
		<tbody>
			{foreach:items = $this->goodsList}
			{set:$type=isset($item['spec_array'])?'product':'goods'}
			{set:$item['id']=isset($item['spec_array'])?$item['product_id']:$item['goods_id']}
			<tr>
				<td><img src="{url:/pic/thumb/img/$item[img]/w/66/h/66}" width="66px" height="66px" alt="{$item['name']}" title="{$item['name']}" /></td>
				<td class="t_l">
					<a href="{url:/site/products/id/$item[goods_id]}" class="blue">{$item['name']}</a>
					{if:isset($item['spec_array'])}
					<p>
					{set:$spec_array=Block::show_spec($item['spec_array']);}
					{foreach:items=$spec_array item=$specValue key=$specName}
						{$specName}：{$specValue} &nbsp&nbsp
					{/foreach}
					</p>
					{/if}
				</td>
				<td>{$item['point']}</td>
				<td><b>￥{$item['sell_price']}</b></td>
				<td>减￥{$item['reduce']}</td>
				<td>
					<div class="num">
						{set:$item_json = JSON::encode($item)}
						<a class="reduce" href="javascript:void(0)" onclick='cart_reduce("{$type}",{$item_json});'>-</a>
						<input class="tiny" value="{$item['count']}" onblur='cartCount("{$type}",{$item_json});' type="text" id="{$type}_count_{$item['id']}" />
						<a class="add" href="javascript:void(0)" onclick='cart_increase("{$type}",{$item_json});'>+</a>
					</div>
				</td>
				<td>￥<b class="red2" id="{$type}_sum_{$item['id']}">{$item['sum']}</b></td>
				<td><a href="{url:/simple/removeCart/link/cart/type/$type/goods_id/$item[id]}">删除</a></td>
			</tr>
			{/foreach}

			<tr class="stats">
				<td colspan="8">
					<span>商品总重量：<b id='weight'>{$this->weight}</b>g</span><span>商品总金额：￥<b id='origin_price'>{$this->sum}</b> - 商品优惠：￥<b id='discount_price'>{$this->reduce}</b> - 促销活动优惠：￥<b id='promotion_price'>{$this->proReduce}</b></span><br />
					金额总计（不含运费）：￥<b class="orange" id='sum_price'>{$this->final_sum}</b>
				</td>
			</tr>
		</tbody>
		<tfoot>
			<tr>
				<td colspan="2" class="t_l">
					{if:$this->user['user_id']}
					<a class="file" href="{url:/simple/deposit_cart_set}">寄存购物车</a>
					<a class="file" href="javascript:deposit_cart_get();">取出购物车</a>
					<a class="file" href="javascript:void(0)" onclick="delModel({msg:'确定要清空寄存购物车么？',link:'{url:/simple/deposit_cart_clear}'});">清空寄存购物车</a>
					{/if}
					<a class="del" href="javascript:void(0);" onclick="delModel({msg:'确定要清空购物车么？',link:'{url:/simple/clearCart}'});">清空购物车</a>
				</td>
				<td colspan="6" class="t_r">
					{set:$callback = IFilter::act(IReq::get('callback'),'text');}
					<a class="btn_continue" href="{url:$callback}">继续购物</a>
					{if:$this->goodsList}
					<a class="btn_pay" href="javascript:check_finish();">去结算</a>
					{/if}
				</td>
			</tr>
		</tfoot>
	</table>

	<div class="box" style="border:0px;"><div class="title" style="font-size:14px;font-weight: bold;">热门商品推荐</div></div>
		<ul id="scrollpic" class="prolist" style="padding-bottom: 20px;float: left;">
			{foreach:items=Api::run('getCommendHot',6)}
			<li>
				<a href="{url:/site/products/id/$item[id]}">
					<img width="190px" height="190px" src="{url:/pic/thumb/img/$item[img]/w/190/h/190}" alt="{$item['name']}" title="{$item['name']}">
				</a>
				<p class="pro_title"><a href="{url:/site/products/id/$item[id]}">"{$item['name']}"</a></p>
				<p class="brown"><b>￥{$item['sell_price']}</b></p>
				<label class="btn_orange2"><input type="submit" onclick="window.location.href='{url:/simple/joinCart/link/cart/type/goods/goods_id/$item[id]}';" value="加入购物车"></label>
			</li>
			{/foreach}
		</ul>
</div>

<script type='text/javascript'>
//购物车数量改动计算
function cartCount(type,obj,oldCount)
{
	var countInput    = $('#'+type+'_count_'+obj.id);
	var countInputVal = parseInt(countInput.val());

	//商品数量大于1件
	if(isNaN(countInputVal) || (countInputVal <= 0))
	{
		alert('购买的数量必须大于1件');
		countInput.val(1);
		cartCount(type,obj,oldCount);
	}

	//商品数量小于库存量
	else if(countInputVal > parseInt(obj.store_nums))
	{
		alert('购买的数量不能大于此商品的库存量');
		countInput.val(parseInt(obj.store_nums));
		cartCount(type,obj,oldCount);
	}
	else
	{
		//修改按钮状态
		countInput.attr('disabled',true);
		$('.btn_pay').val('wait');

		//获取之前的购买数量
		if(oldCount == undefined)
		{
			oldCount = countInput.data('oldCount') ? parseInt(countInput.data('oldCount')) : parseInt(obj.count);
		}

		//修改的购买数量
		var changeNum = parseInt(countInput.val()) - oldCount;

		//商品数量没有改动
		if(changeNum == 0)
		{
			//修改按钮状态
			countInput.attr('disabled',false);
			$('.btn_pay').val('ok');
			return '';
		}

		//更新购物车中此商品的数量
		$.getJSON("{url:/simple/joinCart}",{"type":type,"goods_id":obj.id,"goods_num":changeNum,"random":Math.random()},function(content){
			if(content.isError == true)
			{
				alert(content.message);
				var countInput = $('#'+type+'_count_'+obj.id);

				//上次数量回填
				if(changeNum < 0)
				{
					countInput.val(parseInt(countInput.val() - changeNum));
				}
				else
				{
					countInput.val(parseInt(countInput.val() + changeNum));
				}

				//修改按钮状态
				countInput.attr('disabled',false);
				$('.btn_pay').val('ok');
			}
			else
			{
				var countInput = $('#'+type+'_count_'+obj.id);

				//缓存旧的购买数量
				countInput.data('oldCount',parseInt(countInput.val()));

				/*变动的价格可能为负数(减少购买量)*/
				var smallSumC   = (mathSub(parseFloat(obj.sell_price),parseFloat(obj.reduce))) * changeNum; //价格变动
				var weightC     = mathMul(parseFloat(obj.weight),changeNum);       //重量变动
				var originC     = mathMul(parseFloat(obj.sell_price),changeNum);   //原始价格变动
				var discountC   = mathMul(parseFloat(obj.reduce),changeNum);       //优惠变动

				/*开始更新数据(1)*/

				//商品小结计算
				var smallSum    = $('#'+type+'_sum_'+obj.id);
				smallSum.html(mathAdd(parseFloat(smallSum.text()),smallSumC));

				//最终重量
				$('#weight').html(mathAdd(parseFloat($('#weight').text()),weightC));

				//原始金额
				$('#origin_price').html(mathAdd(parseFloat($('#origin_price').text()),originC));

				//优惠价
				$('#discount_price').html(mathAdd(parseFloat($('#discount_price').text()),discountC));

				//促销规则检测
				var final_sum   = mathSub(parseFloat($('#origin_price').text()),parseFloat($('#discount_price').text()));
				var tmpUrl = '{url:/simple/promotionRuleAjax/random/@random@}';
				tmpUrl = tmpUrl.replace("@random@",Math.random());
				$.getJSON( tmpUrl ,{final_sum:final_sum},function(content){
					if(content.promotion.length > 0)
					{
						$('#cart_prompt .indent').remove();

						for(var i = 0;i < content.promotion.length; i++)
						{
							$('#cart_prompt').append('<p class="indent blue">'+content.promotion[i].plan+'，'+content.promotion[i].info+'</p>');
						}
						$('#cart_prompt').show();
					}
					else
					{
						$('#cart_prompt .indent').remove();
						$('#cart_prompt').hide();
					}
					/*开始更新数据 (2)*/

					//促销活动
					$('#promotion_price').html(content.proReduce);

					//最终金额
					$('#sum_price').html(mathSub(mathSub(parseFloat($('#origin_price').text()),parseFloat($('#discount_price').text())),parseFloat($('#promotion_price').text())));

					//修改按钮状态
					countInput.attr('disabled',false);
					$('.btn_pay').val('ok');
				});
			}
		});
	}
}

//增加商品数量
function cart_increase(type,obj)
{
	//库存超量检查
	var countInput = $('#'+type+'_count_'+obj.id);
	var oldCount   = parseInt(countInput.val());
	if(parseInt(countInput.val()) + 1 > parseInt(obj.store_nums))
	{
		alert('购买的数量大于此商品的库存量');
	}
	else
	{
		if(countInput.attr('disabled') == true)
		{
			return false;
		}
		else
		{
			countInput.attr('disabled',true);
		}
		countInput.val(parseInt(countInput.val()) + 1);
		cartCount(type,obj,oldCount);
	}
}

//减少商品数量
function cart_reduce(type,obj)
{
	//库存超量检查
	var countInput = $('#'+type+'_count_'+obj.id);
	var oldCount   = parseInt(countInput.val());
	if(parseInt(countInput.val()) - 1 <= 0)
	{
		alert('购买的数量必须大于1件');
	}
	else
	{
		if(countInput.attr('disabled') == true)
		{
			return false;
		}
		else
		{
			countInput.attr('disabled',true);
		}
		countInput.val(parseInt(countInput.val()) - 1);
		cartCount(type,obj,oldCount);
	}
}

//检测购买完成量
function check_finish()
{
	if($.trim($('.btn_pay').val()) == 'wait')
	{
		window.setInterval("check_finish()", 400);
	}
	else
	{
		window.location.href = '{url:/simple/cart2}';
	}
}

/**
 * 读取购物车
 */
function deposit_cart_get()
{
	$.getJSON('{url:/simple/deposit_cart_get}',{"random":Math.random()},function(json)
	{
		if(json.isError == 1)
		{
			alert('读取购物车失败');
			return;
		}
		//页面刷新
		window.location.reload();
	});
}
</script>

